package com.adminpcDao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.adminpcMod.*;


public class FileDao {
	
	private List<File > file;
	//private static List UserFile;
	public List getUserFile(Long identiUs){
	//public static void main(String[] args){
    //long identiUs = 73;		
	Session session = HibernateUtil.getSessionFactory().openSession();
		 Transaction transaction = null;
		 transaction = session.beginTransaction();
		 
		 List UserFile = new ArrayList();
		 List rowEmployee = new ArrayList();
		 List rowFile =new ArrayList();
		 rowEmployee = session.createQuery("SELECT C.identiComputer FROM User U,Computer C  WHERE U.identiUser ='" + identiUs + "'and C.user ='"+ identiUs +"'").list();
		 rowFile = session.createQuery("FROM Computer C,File F  WHERE C.identiComputer ='" + rowEmployee.get(0)+ "'and F.computer ='"+ rowEmployee.get(0) +"'").list();
		 for (int i = 0; i < rowFile.size(); i++)
		 {
		 Object[] RowObj = (Object[]) rowFile.get(i); 
		 File RowFile = (File) RowObj[1];
		 UserFile.add(RowFile);	
		 }
		  transaction.commit();
		 session.close();
		return UserFile;
	}
	//Delet File
	public void deleteFile(Long identiFile){
		  Session session = HibernateUtil.getSessionFactory().openSession();
	       	session.beginTransaction();
	        List file = new ArrayList<File>();
	       	
		     file = session.createQuery("FROM File F WHERE F.identiFile ='" + identiFile +"'").list();
		     Iterator results = file.iterator();	 
			 for (int i = 0; i < 1; i++)
			 {   
		     //Object[] RowObj = (Object[]) file.get(i); 
		   			 
		   	 File RowFile = (File) file.get(0);
	         session.delete(RowFile);
			 }
		            session.getTransaction().commit();
		    		com.util.HibernateUtil.getSessionFactory().close();	
		
	}
    //Update File
	public void updatefile(File updatefile){
		Session session = HibernateUtil.getSessionFactory().openSession();
        
       	session.beginTransaction();
       	session.update(updatefile);
        session.getTransaction().commit();
		com.util.HibernateUtil.getSessionFactory().close();
	}	

}
